package com.hangzhou.xyj.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

/**
 * 自定义带进度条的WebView
 * @author neo.duan
 *
 */
public class CustomWebView extends WebView {
	
	private ProgressBar progressbar;

	public CustomWebView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public CustomWebView(Context context) {
		this(context,null);
	}

	public CustomWebView(Context context, AttributeSet attrs) {
		this(context,attrs,0);
		
	}

	private void init(Context context) {
		progressbar = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal);
        progressbar.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 3, 0, 0));
        addView(progressbar);
		
		WebSettings webSettings = getSettings();
		webSettings.setJavaScriptEnabled(true);

		// 自适应屏幕
		webSettings.setUseWideViewPort(true);
		webSettings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL);
		webSettings.setLoadWithOverviewMode(true);

		// 设置可以支持缩放
		webSettings.setSupportZoom(true);
		webSettings.setBuiltInZoomControls(true);

		setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);		
		setWebViewClient(new CommonWebViewClient());
		setWebChromeClient(new CommonWebChromeClient());
	}
	
	class CommonWebViewClient extends WebViewClient {
		
	}
	
	class CommonWebChromeClient extends WebChromeClient {
		@Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                progressbar.setVisibility(GONE);
            } else {
                if (progressbar.getVisibility() == GONE)
                    progressbar.setVisibility(VISIBLE);
                progressbar.setProgress(newProgress);
            }
            super.onProgressChanged(view, newProgress);
        }
	}
	
}
